import { getClassAndStyleList } from '../../../../api/index'

Component({
  properties: {
    show: {
      type: Boolean,
      value: false
    },
    active: {
      type: String,
      value: ''
    }
  },
  data: {
    columns: []
  },
  methods: {
    beforeOpen () {
      this.getList()
    },
    select ({ currentTarget: { dataset: { item, index } } }) {
      let key = `columns[${index}].checked`
      this.setData({
        [key]: !item.checked
      })
    },
    confirm () {
      let selections = this.data.columns.filter(item => item.checked)
      if (selections.length) {
        this.triggerEvent('confirm', selections)
      } else {
        wx.showToast({ title: '请先选择企业类型！', icon: 'none' })
      }
    },
    cancel () {
      this.triggerEvent('close')
    },
    async getList () {
      try {
        const { ResultSet } = await getClassAndStyleList()
        let actives = this.data.active.split(',')
        ResultSet.userData.firstClassificationList.forEach(item => {
          item.checked = false
          actives.forEach(id => {
            if (item.classificationId === parseInt(id)) item.checked = true
          })
        })
        this.setData({ columns: ResultSet.userData.firstClassificationList })
      } catch (err) {
        wx.showToast({ title: err.message || err.ResultSet.errors, icon: 'none' })
      }
    }
  },
})